<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\coupon\Coupon;
use app\common\model\coupon\Log;
use app\common\model\Shop as ModelShop;
use app\common\model\User;

/**
 * 自提点接口
 */
class Shop extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    /**
     * 自提点列表
     *
     */
    public function list()
    {
        $get = request()->get();
        extract($get);
        if (!$lat || !$lng) {
            $this->error('定位失败');
        }
        $list = ModelShop::field(['*', self::distanceSql($lat, $lng)])->where('status', 'normal')->order('distance ASC')->paginate(10);
        $this->success('请求成功', $list);
    }
    /**
     * 获取排序sql
     * @param $latitude
     * @param $longitude
     * @return mixed
     */
    private static function distanceSql($latitude, $longitude)
    {
        $field = "(round( 6378137 * 2 * asin(sqrt(pow(sin(((lat * pi()) / 180 - ({$latitude} * pi()) / 180) / 2), 2) + cos(({$latitude} * pi()) / 180) * cos((lat * pi()) / 180) * pow(sin(((lng * pi()) / 180 - ({$longitude} * pi()) / 180) / 2), 2))))) AS distance";
        return $field;
    }

   
}
